package com.jstyle.jclife.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.model.NetResultData;
import com.jstyle.jclife.utils.ImageUtils;
import com.jstyle.jclife.utils.NetWorkConast;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.utils.ShareTools;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.tencent.qq.QQ;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/4/11.
 */

public class ShareDialog extends JstyleDialog {
    private static final String TAG = "ShareDialog";
    @BindView(R.id.ll_share_ch)
    LinearLayout llShareCh;
    @BindView(R.id.ll_share_eh1)
    LinearLayout llShareEh1;
    @BindView(R.id.ll_share_eh2)
    LinearLayout llShareEh2;


    private ShareTools shareTools;


    public ShareDialog(@NonNull Context context) {
        super(context);
    }
    String path;
    public ShareDialog(@NonNull Context context, @StyleRes int themeResId,String path) {
        super(context, themeResId);
        shareTools = new ShareTools(context,path);
        this.path=path;
    }

    protected ShareDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.dialog_share);
        init();
    }

    private void init() {

        initWidth(ScreenUtils.getScreenWidth(getContext()));
        initHeight(ScreenUtils.getScreenHeight(getContext()));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Locale locale = getContext().getResources().getConfiguration().locale;
        String language = locale.getLanguage();

        llShareCh.setVisibility(language.equals("zh")?View.VISIBLE:View.GONE);
        llShareEh1.setVisibility(language.equals("zh")?View.GONE:View.VISIBLE);
        llShareEh2.setVisibility(language.equals("zh")?View.GONE:View.VISIBLE);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @OnClick({R.id.bt_share_qq,R.id.bt_share_weibo,R.id.bt_share_wechat_ch,R.id.bt_share_more, R.id.bt_share_facebook_cicle, R.id.bt_share_twitter, R.id.bt_share_Instagram, R.id.bt_share_wechat, R.id.bt_share_whatsapp, R.id.bt_share_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_share_more:
                ScreenUtils.shareByPhone(getContext(),path);
                dismiss();
                break;
            case R.id.bt_share_wechat_ch:

                shareTools.WechartShare("ff");
                break;
            case R.id.bt_share_wechat:

                shareTools.WechartShare("ff");
                break;
            case R.id.bt_share_whatsapp:

                break;
//            case R.id.bt_share_weibo:
//                shareTools.SinaWeiboShare("睡眠详情");
//                break;
            case R.id.bt_share_close:
                dismiss();
                break;
            case R.id.bt_share_facebook_cicle:
                shareTools.Share(Facebook.NAME);
                break;
            case R.id.bt_share_twitter:
                //  shareTools.Share(Twitter.NAME);
                break;
            case R.id.bt_share_Instagram:
                shareLinkedIn();
                //   shareTools.Share(LinkedIn.NAME);
                break;

            case R.id.bt_share_qq:

                shareTools.Share(QQ.NAME);
                break;
        }
        dismiss();
    }

    private void shareLinkedIn() {
        File file = new File(ImageUtils.shootPath+path);
        Map<String, RequestBody> maps = new HashMap<>();
        RequestBody tokenBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis() + "");
        RequestBody appNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), "sleep");
        maps.put(NetWorkConast.KEY_token, tokenBody);
        maps.put(NetWorkConast.KEY_AppName, appNameBody);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part muPart = MultipartBody.Part.createFormData("img", file.getName(), requestBody);
        NetWorkUtil.getInstance().getJstyleApi().updateImage(muPart, maps).map(new Function<NetResultData<String>, Object>() {
            @Override
            public Object apply(NetResultData<String> netResultData) throws Exception {
                int msgCode = netResultData.getMsgCode();
                Log.i(TAG, "apply: " + msgCode);
                if (msgCode == NetResultData.Success_Code) {
                    String url = netResultData.getData();
                    //shareTools.ShareLinkedin(url);
                }
                return new Object();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                Log.i(TAG, "onNext: ");
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: " + e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void startShare() {
        Observable.just("").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String value) {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
